<?php
/**
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
class User extends Common
{
    public function index()
    {
        $html = Db::name('user_admin')->order('id desc')->paginate(15);
        $this->assign('html', $html);
        return view();
    }
    public function indexzu()
    {
        $html = Db::name('user_zu')->order('id desc')->paginate(15);
        $this->assign('html', $html);
        return view();
    }
    //添加权限组
    public function insertzu()
    {
        $data = $this->request->post();
        if (!Db::name('userZu')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新
    public function updatezu()
    {
        $id = input('id');
        $data = $this->request->post();
         if (empty($_POST['qx'])) {
         	$data['qx'] = 0;
         } else {	
            $qx = $_POST['qx'];
            $data['qx'] = implode(',', $qx);
         }	
        if (Db::name('userZu')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    public function edit()
    {
    	$id = input('id');
    	$c = Db::name('userZu')->where('id', $id)->find();
        $this->assign('c', $c);
        return view();
    }
    
    //添加管理员
    public function insertadmin()
    {
        $data = $this->request->post();
        if (Db::name('userAdmin')->where('name', input('name'))->find()) {
            $this->error('用户名存在');
        }
        $key = 'www.115cms.com';
        $data['time'] = time();
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['name'] = xss(input('name'));
        $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        if (!Db::name('userAdmin')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //更新管理员
    public function updateadmin()
    {
    	$data = $this->request->post();
    	$key = 'www.115cms.com';
        $id = input('id');
        $data['time'] = time();
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['name'] = xss(input('name'));
         if (empty(input('password'))) {
         	$data['password'] =  fun_sc_p('userAdmin', array('id'=>$id), 'password');
         } else {	
         	$data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
         }	
        if (Db::name('userAdmin')->where('id', $id)->data($data)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
}